package com.ruitu.mall_98.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.BaseFragment;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98.R;
import com.ruitu.mall_98.activity.SysMsgListActivity;
import com.ruitu.mall_98_1.product.activity.ProductDetailActivity;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.CommonUtils;
import com.ruitu.router_module.bean.DataAdapter;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.product.Product;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;
import q.rorbin.badgeview.QBadgeView;

/**
 * Created by Administrator on 2018/5/22.
 */
public class MsgFragment extends BaseFragment {
    private TextView tv_title, tv_im, tv_sys;//标题,客服聊天,系统消息
    private RelativeLayout rl_sys, rl_chat;//系统消息,客服消息
    private RecyclerView recyclerView;
    private QBadgeView qBadgeViewRong, qBadgeViewSys;//小红点(融云,系统)

    public static MsgFragment newInstance() {
        Bundle args = new Bundle();
        MsgFragment fragment = new MsgFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Arad.bus.register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)//EventBus事件接收方法
    public void eventMsg(EventModel event) {
        if (event.getEventCode() == 18122751) {
            globalAdapter.setDataList(AppHolder.instance().getGlobalProList());
        }
        if (event.getEventCode() == 19010251) {//融云消息数量
            int number = (int) event.getEventObj();
            addBadge(number, qBadgeViewRong, tv_im);
        }
        if (event.getEventCode() == 19010252) {//系统消息数量
            int number = (int) event.getEventObj();
            addBadge(number, qBadgeViewSys, tv_sys);
        }
    }

    private DataAdapter globalAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.home_fragment_msg, container, false);
        tv_title = v.findViewById(R.id.tv_title);
        rl_sys = v.findViewById(R.id.rl_sys);
        rl_chat = v.findViewById(R.id.rl_chat);
        tv_im = v.findViewById(R.id.tv_im);
        tv_sys = v.findViewById(R.id.tv_sys);
        recyclerView = v.findViewById(R.id.recyclerView);
        qBadgeViewRong = new QBadgeView(getActivity());
        qBadgeViewSys = new QBadgeView(getActivity());

        globalAdapter = new DataAdapter(getActivity());
        GridLayoutManager manager1 = new GridLayoutManager(getActivity(), 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(manager1);
        recyclerView.setAdapter(globalAdapter);
        globalAdapter.setDataList(AppHolder.instance().getGlobalProList());
        globalAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                Product product = new Product();
                product.setGoodId(AppHolder.instance().getGlobalProList().get(position).getGoodsId());
                bundle.putSerializable("product", product);
                startActivity(ProductDetailActivity.class, bundle);
            }
        });

        tv_title.setText("消息");

        rl_sys.setOnClickListener(this);
        rl_chat.setOnClickListener(this);

        return v;
    }

    public void addBadge(int number, QBadgeView qBadgeView, View targetView) {// add badge
        try {
            if (number == 0) {
                qBadgeView.hide(true);
//                number = 99;
//                qBadgeView.setBadgeNumber(number)
//                        .setGravityOffset(1, 15, true)
//                        .bindTarget(targetView)
//                        .setBadgeTextSize(10, true);
            } else {
                qBadgeView.setBadgeNumber(number)
                        .setGravityOffset(1, 14, true)
                        .bindTarget(targetView)
                        .setBadgeTextSize(10, true)
//                        .setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
//                            @Override
//                            public void onDragStateChanged(int dragState, Badge badge, View targetView) {
//                                if (Badge.OnDragStateChangedListener.STATE_SUCCEED == dragState) {
////                            Toast.makeText(BadgeViewActivity.this, R.string.tips_badge_removed, Toast.LENGTH_SHORT).show();
//                                }
//                            }
//                        })
                ;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        if (v == rl_chat) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                /**
                 * 启动会话列表界面。
                 * supportedConversation 定义会话列表支持显示的会话类型，及对应的会话类型是否聚合显示。
                 * 例如：supportedConversation.put(Conversation.ConversationType.PRIVATE.getName(), false) 非聚合式显示 private 类型的会话。
                 */
                Map<String, Boolean> typeMap = new HashMap<>();
                typeMap.put(Conversation.ConversationType.PRIVATE.getName(), false);
                RongIM.getInstance().startConversationList(getActivity(), typeMap);
            } else {
                ToastUtils.showShort("请先登录");
            }
        }
        if (v == rl_sys) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                startActivity(SysMsgListActivity.class);
            } else {
                ToastUtils.showShort("请先登录");
            }
        }
    }
}
